package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.qo.EmployeeQueryObject;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.qo.PageResult;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IRoleService;
import cn.wolfcode.util.RequiredPermission;
import cn.wolfcode.util.UserContext;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private IDepartmentService departmentService;

    @Autowired
    private IRoleService roleService;

    //
    @RequestMapping("/list")
    @RequiredPermission(name = "员工列表",expression = "employee:list")
    public String list(Model model,@ModelAttribute("qo") EmployeeQueryObject qo){
        //获取表的分页所有数据
        //System.out.println(pageResult.getTotalPage());
        model.addAttribute("OwndeptId",
                employeeService.get(UserContext.getCurrentUser().getId()).getDept().getId());


        model.addAttribute("user", UserContext.getCurrentUser());

        //将数据存入到对象中
        model.addAttribute("pageInfo", employeeService.query(qo));

        //将部门数据回显到过滤查询
        List<Department> departments = departmentService.listAll();
        model.addAttribute("departments", departments);
        //从对象获取数据,就与视图路径拼接成 html 内容响应回浏览器
        return "/employee/list";
    }

    //删除

    @RequestMapping("/delete")
    @ResponseBody
    @RequiredPermission(name = "员工删除",expression = "employee:delete")
    public JsonResult delete(Long id){

        try {
            if (id != null) {
                employeeService.delete(id);
            }
            return new JsonResult();//success 默认为true
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,"操作失败");
        }

    }


    //去新增的页面
    @RequestMapping("/input")
    @RequiredPermission(name = "员工新增/修改页面",expression = "employee:input")
    public String input(Long id, Model model){

        //回显角色数据
        model.addAttribute("roles", roleService.listAll());

        //将部门数据回显
        List<Department> departments = departmentService.listAll();
        model.addAttribute("departments", departments);
        //判断 id 是否为空,需不需要做回显
        if (id != null) {
            Employee employee = employeeService.get(id);
            model.addAttribute("employee", employee);
        }
        return "employee/input";
    }

    //新增或者修改
    @RequestMapping("/saveOrUpdate")
    @RequiredPermission(name = "员工新增/修改",expression = "employee:saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Employee employee,Long[] ids){


            //判断 id 是否为空,新增还是修改
            if (employee.getId() != null) {
                employeeService.update(employee,ids);
            }else {
                employeeService.save(employee,ids);
            }

            return new JsonResult();

    }


    //员工去修改密码
    @RequestMapping("/updatePwdinput")
    public String updatePwdiniput(){
        return "common/updatePwd";
    }

    //管理员去重置密码
    @RequestMapping("/resetPwdInput")
    public String resetPwdInput(Long id,Model model){

        if (id != null) {
            Employee employee = employeeService.get(id);
            model.addAttribute("employee", employee);
        }
        return "common/resetPwd";
    }


    //修改密码
    @RequestMapping("/updatePwd")
    @ResponseBody
    public JsonResult updatePwd(String oldPassword, String newPassword){


        employeeService.updatePassword(oldPassword,newPassword);
            return new JsonResult();

    }

    //重置密码
    @RequestMapping("/resetPwd")
    @ResponseBody
    public JsonResult resetPwd(Long id, String newPassword){


            if ( ! UserContext.getCurrentUser().isAdmin()){
                return new JsonResult(false,"没有权限操作");
            }
            employeeService.resetPwd(id,newPassword);
            return new JsonResult();

    }

    @RequestMapping("/renew")
    @ResponseBody
    public JsonResult renew(Long id){


        if ( ! UserContext.getCurrentUser().isAdmin()){
            return new JsonResult(false,"没有权限操作");
        }
        employeeService.renew(id);
        return new JsonResult();

    }

    @RequestMapping("/batchDelete")
    @ResponseBody
    public JsonResult batchDelete(Long[] ids){

        employeeService.batchDelete(ids);
        return new JsonResult();

    }

    @RequestMapping("/exportXls")
    public void exportXls(HttpServletResponse response) throws IOException {
        //设置文件下载的响应头,设置后缀名
        response.setHeader("Content-Disposition", "attachment;filename=employee.xls");
       Workbook wb = employeeService.exportXls();
        //把excel的数据输出到浏览器的响应流中
         wb.write(response.getOutputStream());
    }


    @RequestMapping("/checkName")
    @ResponseBody
    public HashMap checkName(Long id,String name){
        //根据名字查询,判断是否能查询数据
        boolean result = employeeService.selectByName(id,name);
        HashMap map = new HashMap();
        map.put("valid", result); //查到就不能通过验证
        return map;
    }

    @RequestMapping("/importXls")
    @ResponseBody
    public JsonResult importXls(MultipartFile file) throws IOException {
        employeeService.importXls(file);
        return new JsonResult();
    }


}
